<template>
  <view class="news-list bg-white px-5.25 pt-1 pb-3 box-border min-h-[100vh]">
    <template v-if="news.list.length">
      <view
        class=""
        v-for="(item, index) in news.list"
        :key="index"
        @click="scanNewsDetail(item)"
      >
        <newsListItem
          :newsTitle="item.title"
          :newsTime="
            item.createTime ? dayjs(item.createTime).format('YYYY-MM-DD') : ''
          "
          :newsImg="imgPrefix + item.imgUrl"
        ></newsListItem>
      </view>
    </template>
    <up-empty
      v-else
      mode="history"
      text="暂无数据"
      icon="http://cdn.uviewui.com/uview/empty/history.png"
    >
    </up-empty>
    <!-- <up-loadmore
      :status="status"
      loadmore-text="上拉加载更多"
      nomore-text="没有更多了"
    /> -->
    <up-toast ref="uToastRef"></up-toast>
  </view>
</template>

<script setup lang="ts">
import newsListItem from "@/components/newsListItem/newsListItem.vue";
import {
  listAntiFraud,
  listFireSafety,
  listPreventiveTip,
  regulationslistPage,
  integritylistPage,
} from "@/api/news/index";
import { imgPrefix } from "@/config/app";
import dayjs from "dayjs";

const uToastRef = <any>ref(null);
const data = reactive({
  news: {
    queryParams: {
      pageNum: 1,
      pageSize: 10,
    },
    current: 0,
    list: <any>[],
    total: 0,
    request: [
      listAntiFraud,
      listFireSafety,
      listPreventiveTip,
      regulationslistPage,
    ],
  },
});
const { news } = toRefs(data);
const status = ref("loadmore");

onLoad((options) => {
  if (options) {
    uni.setNavigationBarTitle({
      title: options.newsTitle,
    });
    switch (options.newsTitle) {
      case "反诈宣传":
        news.value.current = 0;
        break;
      case "消防安全":
        news.value.current = 1;
        break;
      case "防范提示":
        news.value.current = 2;
        break;
      case "政策规定":
        news.value.current = 3;
        break;
      case "工作职责":
        news.value.current = 4;
        break;
      case "服务承诺":
        news.value.current = 5;
        break;
      default:
        break;
    }
    initNews();
  }
});
onReachBottom(() => {
  let allTotal =
    news.value.queryParams.pageNum * news.value.queryParams.pageSize;
  if (allTotal < news.value.total) {
    // status.value = "loading";
    news.value.queryParams.pageNum++;
    initNews();
  } else {
    // status.value = "nomore";
    uToastRef.value.show({
      type: "info",
      message: "暂无更多数据",
    });
    return;
  }
});
// 获取对应的新闻列表
const initNews = () => {
  uni.showLoading({
    title: "加载中",
    mask: true,
  });
  if (news.value.current == 5 || news.value.current == 4) {
    let obj = {...news.value.queryParams,type:news.value.current}
    integritylistPage(obj)
      .then((res: any) => {
        uni.hideLoading();
        const { data } = res;
        data.records = data.records ?? [];
        news.value.list = [...news.value.list, ...data.records];
        news.value.total = data.total;
        uni.stopPullDownRefresh();
      })
      .catch((err: any) => {
        uni.hideLoading();
        uToastRef.value.show({
          type: "error",
          message: err,
        });
      });
  } else {
    news.value.request[news.value.current](news.value.queryParams)
      .then((res: any) => {
        uni.hideLoading();
        const { data } = res;
        data.records = data.records ?? [];
        news.value.list = [...news.value.list, ...data.records];
        news.value.total = data.total;
        uni.stopPullDownRefresh();
      })
      .catch((err: any) => {
        uni.hideLoading();
        uToastRef.value.show({
          type: "error",
          message: err,
        });
      });
  }
};
// 查看新闻详情
const scanNewsDetail = (item: any) => {
  uni.navigateTo({
    url: `/subPages/crowd/news/detail?newsId=${item.id}&newsType=${news.value.current}`,
  });
};
</script>

<style lang="scss" scoped></style>
